/**
 * 数据格式化工具类
 */

import {getLocale} from "../../i18n/i18n";
import {isNumber} from "./TypeUtil"
import {getObjByRule} from "@core/utils/ObjUtil";

// @ts-ignore
const unitList: string[] = ["B", "KB", "MB", "GB", "TB", "PB"],
    unitSizeList: number[] = unitList.map((_item, i) => Math.pow(1024, i));

/**
 * 转换文件大小单位
 * @param size 原始大小 b
 * @return {string} 最接近的单位
 */
// @ts-ignore
const formatFileSize: Function = (size: number): string => {
    if (!size) {
        return "0B"
    }
    let index: number = Math.floor(size.toString().length / 3);
    if (index >= unitList.length) {
        index = unitList.length - 1;
    }
    let unitSize: number = unitSizeList[index];
    // 转换为目标单位大小 保留两位小数
    let targetSize: string = (size / unitSize).toFixed(2)
    return targetSize.concat(unitList[index]);
};

/**
 * 格式化多语言字符串
 * @param str 包含两种语言的字符串  中文|英文
 * @return 当前语言对应的字符串
 */
// @ts-ignore
const formatLangStr: Function = (str: string = "") => {
    if (!str) str;
    let strArr: string[] = str.split("|");
    if (strArr.length === 2) {
        return getLocale(strArr[0], strArr[1]);
    }
    return strArr[0];
};

const formatStatusColor: Function = (status: string) => {
    let backgroundColor: string = "#fff";
    let color: string = "#181d1f";
    switch (status) {
        case "未开始":
        case "notStart":
            backgroundColor = "#fff";
            color = "#181d1f";
            break;
        case "进行中":
        case "处理中":
        case "待关闭":
        case "inProcess":
        case "Processing":
            backgroundColor = "#FFC000";
            color = "#fff";
            break;
        case "完成":
        case "已完成":
        case "Finished":
        case "complete":
        case "批准":
        case "已审批":
        case "已关闭":
        case "Approved":
        case "生效":
        case "Active":
            backgroundColor = "#92D050";
            color = "#fff";
            break;
        case "拒绝":
        case "已拒绝":
        case "待终止":
        case "Rejected":
        case "延期":
        case "delay":
            backgroundColor = "#FBA8B1";
            color = "#fff";
            break;
        case "废弃":
        case "discard":
        case "失效":
        case "Inactive":
        case "已终止":
            backgroundColor = "#BFBFBF";
            color = "#181d1f";
            break;
        case "待执行":
            backgroundColor = "#4d78ec";
            color = "#181d1f";
            break;
        default:
            backgroundColor = "#fff";
            color = "#181d1f";
    }
    return {
        backgroundColor,
        color
    }
}

const formatEditStatusColor = (editStatus: string) => {
    let backgroundColor: string = "";
    switch (editStatus) {
        case 'add':
        case 'create':
        case 'feeding':
            backgroundColor = 'rgba(0, 255, 0, 0.3)'
            break;
        case 'edit':
        case 'change':
        case 'update':
            backgroundColor = 'rgba(255, 255, 0, 0.5)'
            break;
        case 'delete':
        case 'remove':
        case 'notFeed':
            backgroundColor = 'rgba(255, 0, 0, 0.5)'
            break;
        case 'replace':
            backgroundColor = 'rgba(0, 0, 255, 0.3)'
            break;
        default:
            break;
    }
    return {backgroundColor}
}

const formatDeviationColor: Function = (deviation: string) => {
    let backgroundColor: string = "#ff0000";
    let color: string = "#fff";
    switch (deviation) {
        case "red":
            backgroundColor = "#ff0000";
            color = "#fff";
            break;
        case "yellow":
            backgroundColor = "#ecb027";
            color = "#fff";
            break;
        case "green":
            backgroundColor = "#92d050";
            color = "#fff";
            break;
        default:
            backgroundColor = "#fff";
            color = "#181d1f";
    }
    return {
        backgroundColor,
        color
    }
}

const formatNumToLevel: Function = (num: number) => {
    const chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
    const chnUnitSection = ["", "万", "亿", "万亿", "亿亿"];
    const chnUnitChar = ["", "十", "百", "千"];

    function sectionToChinese(section: number) {
        let ori = section,
            strIns = '',
            chnStr = '',
            unitPos = 0,
            zero = true;
        while (section > 0) {
            let v = section % 10;
            if (v === 0) {
                if (!zero) {
                    zero = true;
                    chnStr = chnNumChar[v] + chnStr;
                }
            } else {
                zero = false;
                strIns = chnNumChar[v];
                strIns += chnUnitChar[unitPos];
                chnStr = strIns + chnStr;
            }
            unitPos++;
            section = Math.floor(section / 10);
        }
        if (ori < 20) {
            chnStr = chnStr.replace('一十', '十');
        }
        return chnStr;
    }

    function numChangeStr(num: number) {
        let unitPos = 0,
            strIns = '',
            chnStr = '',
            needZero = false;
        if (num === 0) {
            return chnNumChar[0];
        }
        while (num > 0) {
            let section = num % 10000;
            if (needZero) {
                chnStr = chnNumChar[0] + chnStr;
            }
            strIns = sectionToChinese(section);
            strIns += (section !== 0) ? chnUnitSection[unitPos] : chnUnitSection[0];
            chnStr = strIns + chnStr;
            needZero = (section < 1000) && (section > 0);
            num = Math.floor(num / 10000);
            unitPos++;
        }
        return chnStr;
    }

    if (isNumber(num)) {
        return numChangeStr(num)
    } else {
        return num
    }
}

const copyTypeObj: any = {
    Item: {},
    Bomline: {
        id: "${id}",
        blItemId: "${blItemId}",
        blItemRevId: "${blItemRevId}",
        nameCn:"${blItemRevStrCn}",
        nameEn:"${blItemRevStrEn}",
        type:"${blItemRevType}",
    }
}

const formatCopyList: Function = (list: any[], opt: any = {}) => {
    let initCopyVal: any = {
        id: "id",
        nameCn: "objectStringCn",
        nameEn: "objectStringEn",
        type: "type",
        typeCn: "typeCn",
        typeEn: "typeEn"
    }
    const {
        formatVal = {
            id: "id",
            nameCn: "objectStringCn",
            nameEn: "objectStringEn",
            type: "type",
            typeCn: "typeCn",
            typeEn: "typeEn"
        }
    }: { formatVal: any } = opt;
    return list.map((item: any) => {
        let newRow = getObjByRule(item, {...initCopyVal, ...formatVal})
        if(newRow.id == 0) {
            newRow.id = newRow.blItemRevId
        }
        return newRow
    })
}

const formatTypeCopyList: Function = (list: any[], opt: any = 'type') => {
    let initCopyVal: any = {
        id: "id",
        nameCn: "objectStringCn",
        nameEn: "objectStringEn",
        type: "type",
        typeCn: "typeCn",
        typeEn: "typeEn"
    }
    return list.map((item: any) => {
        let newRow = getObjByRule(item, {...initCopyVal, ...(copyTypeObj[item[opt]] || {})})
        if(newRow.id == 0) {
            newRow.id = newRow.blItemRevId || newRow.blItemId
        }
        return newRow
    })
}

export {
    formatFileSize,
    formatLangStr,
    formatStatusColor,
    formatEditStatusColor,
    formatNumToLevel,
    formatCopyList,
    formatTypeCopyList,
    formatDeviationColor,
    copyTypeObj
};
